//
//  BNRItemsViewController.m
//  Quiz
//
//  Created by YaoJacob on 15/11/3.
//  Copyright © 2015年 Roger. All rights reserved.
//

#import "BNRItemsViewController.h"
#import "BNRItemStore.h"
#import "BNRItem.h"

@interface BNRItemsViewController ()
@property (nonatomic, strong) IBOutlet UIView * headerView;
@end

@implementation BNRItemsViewController

-(instancetype)init
{
    self = [super initWithStyle:UITableViewStylePlain];
    if (self) {
        self.tabBarItem.title = @"表格";
        self.tabBarItem.image = [UIImage imageNamed:@"table"];
        
//        for (int i = 0; i < 5; i++) {
//            [[BNRItemStore sharedStore]CreateItem];
//        }
        
        UINavigationItem* navItem = self.navigationItem;
        navItem.title = @"主页";
        
        UIBarButtonItem* bbi = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addNewItem:)]; //SEL
        
        navItem.rightBarButtonItem = bbi;
        
        navItem.leftBarButtonItem = self.editButtonItem;
    }
    
    return self;
}

-(instancetype)initWithStyle:(UITableViewStyle)style
{
    return [self init];
}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tabBarItem.title = @"表格";
        //        self.tabBarItem.image = [UIImage imageNamed:@"Time"];
    }
    return self;
}

-(void)viewDidLoad
{
    [super viewDidLoad];
    
    //Pool.
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"]; //Pool
    
    UIView* header = self.headerView;
    [self.tableView setTableHeaderView:header];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.tableView reloadData];
}

//获取总数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    int count =[[[BNRItemStore sharedStore]allItems]count];
    count++; //最后一行是固定行
    return  count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //1: new()
    //    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
    
    //2: Pool
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
    
    NSArray * items = [[BNRItemStore sharedStore]allItems];
    if (indexPath.row >= items.count) {
        
        cell.textLabel.text = @"No more items!";
    }
    else
    {
        BNRItem* item = items[indexPath.row];
        
        cell.textLabel.text = item.description;
    }
    
    
    return cell;
}

//-(UIView *)headerView
//{
//    if(!_headerView)
//    {
//        [[NSBundle mainBundle] loadNibNamed:@"HeaderView"
//                                      owner:self
//                                    options:nil];
//    }
//    return _headerView;
//}

-(IBAction)addNewItem:(id)sender
{
    //    NSInteger lastRow = [self.tableView numberOfRowsInSection:0];
    
    BNRItem* item = [[BNRItemStore sharedStore]CreateItem];
    NSInteger lastRow = [[[BNRItemStore sharedStore]allItems] indexOfObject:item];
    
    NSIndexPath * indexPath = [NSIndexPath indexPathForRow:lastRow inSection:0];
    
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
}

//-(IBAction)toggleEditingMode:(id)sender
//{
//    if (self.isEditing) {
//        [sender setTitle:@"Edit" forState:UIControlStateNormal];
//
//        [self setEditing:NO animated:YES];
//    }
//    else
//    {
//
//        [sender setTitle:@"Done" forState:UIControlStateNormal];
//
//        [self setEditing:YES animated:YES];
//    }
//}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //    NSLog(@"%@", editingStyle);
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSArray* arrs = [[BNRItemStore sharedStore]allItems];
        
        //最后一行，有特色用途，不能删除。
        if (indexPath.row >= (arrs.count)) {
            return;
        }
        BNRItem* item = arrs[indexPath.row];
        [[BNRItemStore sharedStore]removeItem:item];
        
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}

-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    NSArray* arrs = [[BNRItemStore sharedStore]allItems];
    //最后一行，有特色用途，不能删除。
    if (sourceIndexPath.row >= (arrs.count)
        || destinationIndexPath.row >= (arrs.count)) {
        
        [tableView reloadData];
        return;
    }
    
    [[BNRItemStore sharedStore]moveItemAtIndex:sourceIndexPath.row toIndex:destinationIndexPath.row];
    
}

//click
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BNRDetailViewController *detailView = [[BNRDetailViewController alloc]init];
    NSArray* arrs = [[BNRItemStore sharedStore]allItems];
    if(indexPath.row >= (arrs.count))
    {
        return;
    }
    
    BNRItem* item = arrs[indexPath.row];
    
    detailView.item =item;
    [self.navigationController pushViewController:detailView animated:YES];
}
@end
